package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.count.KitchenCountDto;
import org.itboys.mobile.service.mongo.mobile.KitchenMobileVillageService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年5月8日
 * 描述：花果山(农厨)_小区_controller_web
 */
@Controller
@RequestMapping("/kitchen/mobile/village")
public class KitchenMobileVillageController extends BaseController{
    @Autowired
    private KitchenMobileVillageService villageService;


    /**
     * 按经纬度，获取小区
     * @param request
     * @param response
     * @param longitude
     * @param latitude
     */
    @RequestMapping(value="/getVillageByLocation",method= RequestMethod.POST)
    @ApiOperation(value="【用户版】按经纬度获取小区。注册时使用")
    public void getVillageByLocation(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="longitude",required=true) String longitude,
                        @RequestParam(value="latitude",required=true) String latitude) {

        try{
        //    Long memberId = AccountIdHoldUtils.getMemberId();
         //   KitchenCountDto result = countService.countBuyNow(memberId, productId, number,isUsedBalance,couponId,villageId);
            List<Map<String,Object>> results = villageService.getVillageByLocation(longitude,latitude);
            ResponseMessage.success(results);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/village/getVillageByLocation----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }

    }
    /**
     * 按经纬度，获取小区
     * @param request
     * @param response

     */
    @RequestMapping(value="/getVillageByTree",method= RequestMethod.POST)
    @ApiOperation(value="【用户版】获取小区(按城市)组装树形结构。已按拼音首字母排序")
    public void getVillageByTree(HttpServletRequest request,
                                     HttpServletResponse response) {

        try{
            List<Map<String,Object>> results = villageService.getVillageByTree();
            ResponseMessage.success(results);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/village/getVillageByTree----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }

    }
    /**
     * 按经纬度，获取小区
     * @param request
     * @param response

     */
    @RequestMapping(value="/getMemberVillage",method= RequestMethod.POST)
    @ApiOperation(value="【用户版】获取用户自己的小区。在首页上。首页接口中也返回此数据")
    public void getMemberVillage(HttpServletRequest request,
                                 HttpServletResponse response) {

        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<Map<String,Object>> results = villageService.getMemberVillage(memberId);
            ResponseMessage.success(results);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/village/getMemberVillage----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }

    }
}
